<?php
/*
 * Copyright (c) 2009 Tom Smith (tom@takeontom.com).
 * 
 * Distributed under the terms of the GNU Lesser General Public License.
 * 
 * ---------------------------------------------------------------------------------
 * 
 * This file is part of Seraph PHP Framework.
 *
 * Seraph PHP Framework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Seraph PHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Seraph PHP Framework.  If not, see <http://www.gnu.org/licenses/>.
*/


class Autoloader	{
	static protected $autoloadType = 'IncludePathAutoloader';
	static protected $autoloaderDir;
	
	static protected $autoloader;
	
	static protected $varDir;
	
	static protected $autoloadTargetDirs = array();
	
	static protected $autoloaders = array();
	
	static public function addAutoloader(IAutoloader $autoloader)	{
		self::$autoloaders[] = $autoloader;
	}
	
	static public function setupAutoloader()	{
		include(self::getAutoloaderDir() . '/__autoload.php');
	}
	
	/**
	 * Attempt to autoload a class with the given name
	 * 
	 * @param $classname
	 * @return bool
	 */
	static public function loadClass($classname)	{
		foreach(self::$autoloaders as $al)	{
			$al->loadClass($classname);
			if (class_exists($classname, false))	{
				return true;
			}
		}
		return false;
	}
	
	static public function setVarDir($varDir)	{
		self::$varDir = $varDir;
	}
	
	static public function setAutoloadTargetDirs(array $dirs)	{
		self::$autoloadTargetDirs = $dirs;
	}
	
	static public function addAutoloadTargetDirs(array $dirs)	{
		self::$autoloadTargetDirs = array_merge(self::$autoloadTargetDirs, $dirs);
		self::al()->addTargetDirs($dirs);
	}
	
	static public function setAutoloaderDir($autoloaderDir)	{
		self::$autoloaderDir = $autoloaderDir;
	}
	static public function getAutoloaderDir()	{
		if (!isset(self::$autoloaderDir))	{
			self::$autoloaderDir = dirname(realpath(__FILE__));
		}
		return self::$autoloaderDir;
	}
	
	static public function setAutoloadType($autoloadType)	{
		self::$autoloadType = $autoloadType;
	}
	static public function getAutoloadType()	{
		return self::$autoloadType;
	}
	
	static protected function loadAutoloader()	{
		$targetFile = self::getAutoloaderDir() . '/' . self::getAutoloadType() . '/' . self::getAutoloadType() . '.php';
		
		if (file_exists($targetFile))	{
			require_once($targetFile);
			
			$c = self::getAutoloadType();
			if (class_exists($c))	{
				self::$autoloader = new $c(self::$autoloadTargetDirs);
				return;
			}
			
			throw new RuntimeException('Autoloader class ' . self::getAutoloadType() . ' was not found');
		}
		
		throw new RuntimeException('Autoloader file ' . self::getAutoloadType() . ' was not found');
	}
}
?>